package com.police;

import android.app.Application;
import android.content.Context;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;

/**
 * Created by Leo on 13-5-29.
 */
public class PoliceApp extends Application {
    public static PoliceApp App;
    // 百度MapAPI的管理类
    public BMapManager mBMapMan = null;
    // 授权Key
    // 申请地址：http://developer.baidu.com/map/android-mobile-apply-key.htm
    //2F27AE258642C2401986A0B1FC860DED54BE4E37
    public static String mStrKey = "71497A46D930B00D63D0F501DC88E56AED557CA4";
    public boolean m_bKeyRight = true; // 授权Key正确，验证通过

    @Override
    public void onCreate() {
        // CrashHandler crashHandler = CrashHandler.getInstance();
        // // 注册crashHandler
        // crashHandler.init(getApplicationContext());
        App = this;
        initEngineManager(this);
        super.onCreate();
    }

    public void initEngineManager(Context context) {
        if (mBMapMan == null) {
            mBMapMan = new BMapManager(context);
        }
        if (!mBMapMan.init(mStrKey, new MyGeneralListener())) {
            Toast.makeText(App.getApplicationContext(), "BMapManager 初始化错误!",
                    Toast.LENGTH_LONG).show();
        }
    }

    // 常用事件监听，用来处理通常的网络错误，授权验证错误等
    public static class MyGeneralListener implements MKGeneralListener {

        @Override
        public void onGetNetworkState(int iError) {
            if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
                Toast.makeText(App.getApplicationContext(), "您的网络出错啦！",
                        Toast.LENGTH_LONG).show();
            } else if (iError == MKEvent.ERROR_NETWORK_DATA) {
                Toast.makeText(App.getApplicationContext(), "输入正确的检索条件！",
                        Toast.LENGTH_LONG).show();
            }
            // ...
        }

        @Override
        public void onGetPermissionState(int iError) {
            if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
                // 授权Key错误：
                Toast.makeText(App.getApplicationContext(),
                        "请输入正确的授权Key！",
                        Toast.LENGTH_LONG).show();
                App.m_bKeyRight = false;
            }
        }
    }

    @Override
    // 建议在您app的退出之前调用mapadpi的destroy()函数，避免重复初始化带来的时间消耗
    public void onTerminate() {
        if (mBMapMan != null) {
            mBMapMan.destroy();
            mBMapMan = null;
        }
        super.onTerminate();
    }
}
